package com.douding.server.service;

import com.douding.server.domain.Chapter;
import com.douding.server.domain.ChapterExample;
import com.douding.server.domain.Course;
import com.douding.server.dto.ChapterDto;
import com.douding.server.dto.ChapterPageDto;
import com.douding.server.dto.CourseDto;
import com.douding.server.dto.PageDto;
import com.douding.server.mapper.ChapterMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ChapterService {

    @Resource
    private ChapterMapper chapterMapper;

    public void list(ChapterPageDto chapterPageDto) {


    }// method


    //新增数据
    private void insert(Chapter chapter) {

    }

    //更新数据
    private void update(Chapter chapter) {

    }

    public void delete(String id) {

    }


    /**
     * 查询某一课程下的所有章
     */
    public List<ChapterDto> listByCourse(String courseId) {

        return null;
    }


    /**
     * 我做的部分   开始
     */
    public void selectByPage(ChapterPageDto chapterPageDto) {
        PageHelper.startPage(chapterPageDto.getPage(), chapterPageDto.getSize());
        List<Chapter> chapterList = chapterMapper.selectList();
        PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
        chapterPageDto.setTotal(pageInfo.getTotal());
        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);
        chapterPageDto.setList(chapterDtoList);
    }

    public void save(ChapterDto chapterDto) {
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterDto, chapter);
        if (chapter.getId() != null) {
            // 更新
            chapterMapper.update(chapter);
        } else {
            String id = UuidUtil.getShortUuid();
            chapter.setId(id);
            // 新增
            chapterMapper.insert(chapter);
        }
    }

    public void deleteById(String id) {
        chapterMapper.deleteById(id);
    }

}//end class
